//
//  VipInfo2ViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/27.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VipInfo2ViewController.h"
#import "VipInfoCardTableViewCell.h"
//#import "SelectVipCardClassViewController.h"
//#import "VipCardInfoViewController.h"

@interface VipInfo2ViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    int startIndex;
    
    int pageSize;
}

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,strong)NSString *totalCount;

@end

@implementation VipInfo2ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    self.view.backgroundColor = BackGroundColor;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    UIButton *buyBtn = [[UIButton alloc] init];
    [buyBtn setTitle:@"购买会员卡"];
    [buyBtn setImage:@"icon_mjia"];
    [buyBtn setTitleColor:CSColorFromRGB(0x0a0a0a)];
    buyBtn.titleLabel.font = MidBigFont;
    buyBtn.backgroundColor = CSWhiteColor;
    [buyBtn addTarget:self action:@selector(addCard)];
    [self.view addSubview:buyBtn];
    [buyBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.width.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view);
        make.height.mas_equalTo(50);
    }];
    [buyBtn imageOnTheTitleRightWithSpace:10];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[VipInfoCardTableViewCell class] forCellReuseIdentifier:@"VipInfoCardTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = BackGroundColor;
    [self.view addSubview:tableView];
    self.tableView= tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.width.mas_equalTo(self.view);
        make.bottom.mas_equalTo(buyBtn.mas_top);
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    footer.automaticallyHidden = YES;
    
    
    MyTableHead *mj_head = [MyTableHead headerWithRefreshingTarget:self refreshingAction:@selector(loadStartData)];
    tableView.mj_header = mj_head;
    tableView.mj_footer = footer;
    [tableView.mj_header beginRefreshing];
}


#pragma mark 下拉
-(void)loadStartData{
    
    startIndex = 1;
    
    pageSize = 20;
    
    [self loadData];
}

#pragma mark 上拉
-(void)loadMoreData{
    
    startIndex = startIndex + 1;
    
    [self loadData];
}

-(void)loadData{
    
    //GET_Leave_DeleteLeave
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    //[MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    VenueModel *venueModel = [VenueModel venue];
    
//    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
//    
//    [params setObject:[[NSUserDefaults standardUserDefaults] objectForKey:@"ShopID"] forKey:@"ShopID"];
//    
//    [params setValue:self.venueVipModel.MemberID forKey:@"MemberID"];
//    
//    [params setObject:[NSString stringWithFormat:@"%d",startIndex] forKey:@"page"];
//    
//    [params setObject:[NSString stringWithFormat:@"%d",pageSize] forKey:@"rows"];
    
    [HTTPTool GET:GET_SalesOrder_SalesOrderListData parameters:params
     
          success:^(id responseObject){
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count > 0) {
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshing];
                  }
                  else{
                      
                      [self.tableView.mj_header endRefreshing];
                      
                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                  }
                  
                  self.noDataView = [NoDataView shareInstance];
                  
                  [self.noDataView hideInView:self.view];
                  
                  if (startIndex == 1) {
                      
                      self.dataSource = [NSMutableArray arrayWithArray:resultArray];
                      
                      if (self.dataSource.count == 0) {
                          
                          self.noDataView = [NoDataView shareInstance];
                          
                          [self.noDataView showInView:self.view];
                      }
                  }
                  else{
                      
                      [self.dataSource addObjectsFromArray:resultArray];
                  }
                  
                  self.totalCount = [NSString stringWithFormat:@"%@",[responseObject objectForKey:@"total"]];
                  
                  [self.tableView reloadData];
              }
              
          }
     
          failure:^(NSError *error){
              
              [self.tableView.mj_header endRefreshing];
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
}

#pragma mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

#pragma mark UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.00001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headView = [[UIView alloc] init];
    headView.backgroundColor = CSWhiteColor;
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = MidBigFont;
    titleLabel.text = @"会员卡种数：";
    titleLabel.textColor = TextColor1;
    [headView addSubview:titleLabel];
    [titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(headView).offset(15);
        make.centerY.mas_equalTo(headView);
    }];
    
    UILabel *countLabel = [[UILabel alloc] init];
    countLabel.font = MidBigFont;
    countLabel.text = self.totalCount;
    [headView addSubview:countLabel];
    [countLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(titleLabel);
        make.left.mas_equalTo(titleLabel.mas_right).offset(5);
    }];
    return headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VipInfoCardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VipInfoCardTableViewCell"];
    
    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
    
    VipCardModel *vipCardModel = [VipCardModel mj_objectWithKeyValues:dict];
    
    cell.vipCardModel = vipCardModel;
    
    cell.selectRow = indexPath.row;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //c'z'q
//    VipCardInfoViewController *vipCardInfoVC = [[VipCardInfoViewController alloc] init];
//
//    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
//
//    VipCardModel *vipCardModel = [VipCardModel mj_objectWithKeyValues:dict];
//
//    if ([vipCardModel.Status intValue] != 0) {
//
//        [MBProgressHUD showText:@"该会员卡已经过期"];
//
//        return;
//    }
//
//    vipCardInfoVC.selectRow = indexPath.row;
//
//    vipCardInfoVC.vipCardModel = vipCardModel;
//
//    [self.navigationController pushViewController:vipCardInfoVC animated:YES];
}


#pragma mark 添加会员卡
-(void)addCard{
    //czq
//    SelectVipCardClassViewController *selectVipCardVC = [[SelectVipCardClassViewController alloc] init];
//
//    [self.navigationController pushViewController:selectVipCardVC animated:YES];
}

#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
        
    }
    
    return _dataSource;
}


-(void)dealloc{
    
    [NotificationCenter removeObserver:self];
}

@end
